package servlet;

import bean.Movie;
import service.impl.MovieServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/find_movie")
public class FindMovie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); // 设置请求编码为UTF-8
        response.setContentType("text/html;charset=UTF-8"); // 设置响应内容类型和编码
        String key = request.getParameter("movieNameKey");
        MovieServiceImpl movieService = new MovieServiceImpl();
        List<Movie> movieList = movieService.getMoviesByKey(key);
        request.getSession().setAttribute("findMovie",movieList);
        if(movieList.size()== 0){
            request.setAttribute("message","数据库没有对应的电影");
            request.setAttribute("page","index.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }else{
            response.sendRedirect("index.jsp");
        }
    }
}
